package org.example.threadobjectclasscommonmethods;

/**
 * 描述：展示wait和notify的基本用法 1.研究代码的执行顺序 2证明wait释放锁
 */
public class Wait {

  static final Object object = new Object();

  public static void main(String[] args) {
    Thread1 thread1 = new Thread1();
    Thread2 thread2 = new Thread2();
    thread1.start();
    thread2.start();
  }

  static class Thread1 extends Thread {

    @Override
    public void run() {
      synchronized (object) {
        System.out.println("线程：" + Thread.currentThread().getName() + "已经执行了");
        try {
          object.wait();
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
        System.out.println("线程：" + Thread.currentThread().getName() + "获取到了锁");
      }
    }
  }

  static class Thread2 extends Thread {

    @Override
    public void run() {
      synchronized (object) {
        System.out.println("线程：" + Thread.currentThread().getName() + "已经执行了");
        object.notify();
        System.out.println("线程：" + Thread.currentThread().getName() + "已经调用notify()");
      }
    }
  }
}
